﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class SequencesSetting : BasePage
    {
        private Qs_SequencesBLL qsb = new Qs_SequencesBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
                //初始化GridView数据
                BindProjectName(false);
            }
        }

        private void Bind()
        {
            ViewState["Where"] = txtKey.Text.Trim();

            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "";
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = qsb.GetRecordCount(Where);

            DataTable dtItem;

            dtItem = qsb.GetListByPage(Where, "c_table_name", PageIng.CurrentPage, PageIng.RowsPerPage);

            if (dtItem != null && dtItem.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = null;
                grvShow.DataBind();
                PageIng.Visible = false;
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Response.Redirect("/BusinessExpansion/Templete/SequencesSettingEdit.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    var table = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                    var col = "";
                    var url = "/BusinessExpansion/Templete/SequencesSettingEdit.aspx?TABLE={0}&COL={1}";

                    url = string.Format(url, HttpUtility.UrlEncode(table), HttpUtility.UrlEncode(col));
                    Response.Redirect(url);
                    return;
                }
            }
        }

        protected void DELETE_CLICK(object sender, EventArgs e)
        {
            try
            {
                int count = 0;
                for (int i = 0; i < grvShow.Rows.Count; i++)
                {
                    if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                    {
                        var table = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                        var col = "";
                        if (qsb.Delete(table, col))
                        {
                            count++;
                        }
                    }
                }
                if (count == 0)
                {
                    MessageBox.Show(this, "【温馨提示】请选择行！");
                    return;
                }
                Bind();
                MessageBox.Show(this, "【温馨提示】你成功删除了" + count + "条！");
            }
            catch (Exception)
            {
                MessageBox.Show(this, "【温馨提示】异常！");
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        /// <summary>
        /// GridView标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindProjectName(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        public void BindProjectName(bool iSsort)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "";
            }

            int rowcount = 0;

            rowcount = qsb.GetRecordCount(Where);

            DataTable dtItem;

            dtItem = qsb.GetListByPage(Where, "c_table_name", PageIng.CurrentPage, PageIng.RowsPerPage);

            DataView view = dtItem.DefaultView;
            if (iSsort)
            {
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                view.Sort = sort;//设置排序
            }

            grvShow.DataSource = view;

            grvShow.DataBind();
        }
    }
}